import { Accessor, onCleanup, Signal } from "solid-js";

export default function clickOutside(
  el: HTMLElement,
  accessor: Accessor<Signal<boolean>>
) {
  const onClick = (e: Event) => !el.contains(e.target) && accessor()?.();
  document.body.addEventListener("click", onClick);
  onCleanup(() => document.body.removeEventListener("click", onClick));
}
